import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  data: {
    id: '',
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    mobile: '',
    description: '',
    repairItemName: '请选择维修项目',
    repairItemId: '',
    houseName: '请选择报修房屋',
    houseId: '',
    appointment: '请选择上门日期',
    repairItem: [],
    attachment: [],
    type: '',
  },
  rules: {
    mobile: [
      { required: true, message: '请填写手机号码!' },
      {
        pattern: /^1[3456789]\d{9}$/,
        message: '请检查手机号码是否正确!',
      },
    ],
    repairItemId: [
      { required: true, message: '请选择维修项目！' },
      { pattern: /^(?!请选择维修项目).*/, message: '请选择维修项目！' },
    ],
    houseId: [
      { required: true, message: '请选择保修房屋!' },
      { pattern: /^(?!请选择报修房屋).*/, message: '请选择保修房屋!' },
    ],
    appointment: [
      { required: true, message: '请选择上门日期!' },
      { pattern: /^(?!请选上门日期).*/, message: '请选上门日期!' },
    ],
    description: [
      { required: true, message: '请输入问题描述!' },
      { pattern: /^(?!请输入问题描述!).*/, message: '请输入问题描述!' },
    ],
  },
  onLoad(params) {
    const type = params.type
    if (type === 'modify') {
      const { id, appointment, attachment, description, houseInfo, mobile, repairItemName, houseId, repairItemId } =
        JSON.parse(params.Info)
      wx.setNavigationBarTitle({
        title: '修改保修信息',
      })
      console.log('yes')
      this.setData({
        type,
        id,
        appointment,
        attachment,
        description,
        houseName: houseInfo,
        mobile,
        repairItemName,
        houseId,
        repairItemId,
      })
    }
  },
  async onShow() {
    const { data } = await wx.http.get('/house')
    this.setData({
      houseList: data,
    })
    const { data: Data } = await wx.http.get('/repairItem')
    this.setData({
      repairItem: Data,
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  selectHouseInfo(e) {
    this.setData({
      houseId: e.detail.id,
      houseName: e.detail.name,
      houseLayerVisible: false,
    })
  },
  selectFixInfo(e) {
    this.setData({
      repairItemId: e.detail.id,
      repairItemName: e.detail.name,
      repairLayerVisible: false,
    })
  },
  selectDateInfo(e) {
    const date = new Date(e.detail)
    const year = date.getFullYear()
    const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
    const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    this.setData({
      appointment: `${year}-${month}-${day}`,
    })
    this.setData({ dateLayerVisible: false })
  },
  async addFile(e) {
    const that = this

    await wx.getImageInfo({
      src: e.detail.file.tempFilePath,
      success: function (res) {
        // success

        wx.uploadFile({
          url: 'https://live-api.itheima.net/upload',
          filePath: res.path,
          name: 'file',
          header: {
            Authorization: `Bearer ${wx.getStorageSync('token')}`,
          },
          // header: {}, // 设置请求的 header
          // formData: {}, // HTTP 请求中其他额外的 form data
          success: function (res) {
            // success

            const obj = JSON.parse(res.data).data
            that.setData({
              attachment: [...that.data.attachment, obj],
            })
          },
          fail: function () {
            // fail
          },
          complete: function () {
            // complete
          },
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      },
    })
  },
  deleteFile(e) {
    const index = e.detail.index
    this.data.attachment.splice(index, 1)
    // 更新 this.data 上的数据，这会自动触发界面更新
    this.setData({
      attachment: this.data.attachment,
    })
  },
  async goList() {
    const valid = this.validate()
    console.log(valid)
    const data = {
      houseId: this.data.houseId,
      repairItemId: this.data.repairItemId,
      mobile: this.data.mobile,
      appointment: this.data.appointment,
      description: this.data.description,
      attachment: this.data.attachment,
    }
    let res
    if (this.data.type === 'modify') {
      const id = this.data.id
      res = await wx.http.post('/repair', { id, ...data })
    } else {
      res = await wx.http.post('/repair', data)
    }
    if (res.code === 10000) {
      wx.reLaunch({
        url: '/repair_pkg/pages/list/index',
      })
    } else {
      wx.utils.toast('提交信息失败')
    }
  },
})
